#include "app_factory.h"

#define APP_FACTORY_PIN             LL_GPIO_PIN_13
#define APP_FACTORY_PORT            GPIOA
#define APP_FACTORY_ReadLevel()     LL_GPIO_IsInputPinSet(APP_FACTORY_PORT, APP_FACTORY_PIN)

#define APP_FACTORY_MOTOR_STOP      0x00
#define APP_FACTORY_MOTOR_FORWARD   0x01
#define APP_FACTORY_MOTOR_REVERSE   0x02
#define APP_FACTORY_MOTOR_TIMEOUT   5 * 1000

typedef struct {
    unsigned char btn;
    unsigned char motor_inited;
    unsigned char motor_direc;
    unsigned int motor_time;
} priv_factory_t;
volatile static priv_factory_t priv_factory = {0x00};
void app_factory_init(void)
{
    LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);

    LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin     = APP_FACTORY_PIN; 
    GPIO_InitStruct.Mode    = LL_GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull    = LL_GPIO_PULL_UP;
    LL_GPIO_Init(APP_FACTORY_PORT, &GPIO_InitStruct);

    if (RESET == APP_FACTORY_ReadLevel()) {
        dev_obj->factory.state  = FACTORY_ENTER;
    } else {
        dev_obj->factory.state  = FACTORY_NONE;
    }

    priv_factory.motor_inited = 0x00;
    LL_GPIO_DeInit(APP_FACTORY_PORT);
}

void app_facotry_motor_cb_handler(void)
{
    priv_factory.motor_direc = APP_FACTORY_MOTOR_STOP;
    app_motor_stop();
}

void app_factory_motor_init(void)
{
    LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOF);

    LL_GPIO_InitTypeDef GPIO_InitStruct = {0x00};
    GPIO_InitStruct.Pin         = LL_GPIO_PIN_4;
    GPIO_InitStruct.Mode        = LL_GPIO_MODE_OUTPUT;
    GPIO_InitStruct.Speed       = LL_GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.OutputType  = LL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.Pull        = LL_GPIO_PULL_DOWN;
    LL_GPIO_Init(GPIOF, &GPIO_InitStruct);
    LL_GPIO_ResetOutputPin(GPIOF, LL_GPIO_PIN_4);

    priv_factory.motor_inited = 0x01;
    app_motor_cb_register(app_facotry_motor_cb_handler);
}

void app_factory_wifi_ready_cb_handler(const unsigned char *data, unsigned short length)
{
    if (dev_obj->factory.state == FACTORY_ENTER && (NULL == data || 0 == length)) {
        app_uart_tx_scan_router();
    }
}

void app_factory_wifi_rssi_cb_handler(const unsigned char *data, unsigned short length)
{
    if ((dev_obj->factory.state == FACTORY_ENTER) && (NULL != data) && (0x02 == length)) {
        if (data[0] == 0x01 && data[1] >= 60) {
            // 扫描成功，并且信号强度大于60%
            app_led_on(APP_LED_WIFI);
        }
    }
}


void app_factory_btn_pressed_cb_handler(app_btn_t btn, app_btn_state_t state)
{
    if (dev_obj->factory.state == FACTORY_ENTER) {
        priv_factory.btn |= (1 << btn);
    }
}

void app_factory_task(void)
{
    if (dev_obj->factory.state != FACTORY_ENTER) {
        return ;
    }

    if (priv_factory.motor_inited == 0x00) {
        app_factory_motor_init();
    }

    if (priv_factory.btn == 0x07) {
        priv_factory.btn = 0x00;
        priv_factory.motor_direc = APP_FACTORY_MOTOR_FORWARD;
        priv_factory.motor_time  = app_sys_ticks;

        app_motor_forward();
    }

    if (priv_factory.motor_direc == APP_FACTORY_MOTOR_FORWARD 
        && time_after(app_sys_ticks, priv_factory.motor_time + APP_FACTORY_MOTOR_TIMEOUT)) {
        priv_factory.motor_direc = APP_FACTORY_MOTOR_REVERSE;
        priv_factory.motor_time  = app_sys_ticks;

        app_motor_reverse();
    }

    if (priv_factory.motor_direc == APP_FACTORY_MOTOR_REVERSE 
        && time_after(app_sys_ticks, priv_factory.motor_time + APP_FACTORY_MOTOR_TIMEOUT)) {
        priv_factory.motor_direc = APP_FACTORY_MOTOR_FORWARD;
        priv_factory.motor_time  = app_sys_ticks;

        app_motor_forward();
    }

    LL_GPIO_SetOutputPin(GPIOF, LL_GPIO_PIN_4);
    int value = app_adc_convert(LL_ADC_CHANNEL_5);
    if (value > 0x0E60) {
        app_led_on(APP_LED_BAT);
    } else {
        app_led_off(APP_LED_BAT);
    }
    LL_GPIO_ResetOutputPin(GPIOF, LL_GPIO_PIN_4);
}
